﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RooHui.Infrastructure.Entities.Entity;
using RooHui.Tool;
using RooHui.Infrastructure.Entities.EFContext;
using RooHui.Infrastructure.Entities.Enum;

namespace RooHui.OA.Biz
{
    public class ProcessStepBiz
    {
        public static List<ProcessStep> GetProcessStepListByPageAndDefID(int page, int pageSize, int defID, ref int count)
        {
            try
            {
                using (Context ctx = new Context())
                {

                    var expression = from p in ctx.ProcessStep
                                     join c in ctx.Process
                                     on p.CurrentProcessID equals c.ID into dt
                                     from c in dt.DefaultIfEmpty()
                                     join n in ctx.Process
                                     on p.NextProcessID equals n.ID into di
                                     from n in di.DefaultIfEmpty()
                                     where p.ProcessDefID == defID && p.Status != (int)ItemStatus.Delete
                                     orderby p.ID
                                     select new { p, c,n };

                    count = expression.Count();
                    var data = expression
                        .Skip((page - 1) * pageSize)
                        .Take(pageSize)
                        .ToList().Select(p => new ProcessStep().Set(p.p).LoadCurrentProcess(p.c).LoadNextProcess(p.n)).ToList();
                    return data;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        public static ProcessStep AddProcessStep(int processDefID, string stepName, int currentProcessID, int nextProcessID)
        {
            try
            {
                var processStep = new ProcessStep()
                {
                    StepName = stepName,
                    ProcessDefID = processDefID,
                    CurrentProcessID = currentProcessID,
                    NextProcessID = nextProcessID,
                    Status = ItemStatus.Enable
                };

                processStep = processStep.PostAdd();
                return processStep;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        public static ProcessStep EditProcessStep(int processStepID, int processDefID, string stepName, int currentProcessID, int nextProcessID)
        {
            try
            {
                var processStep = new ProcessStep().Find(processStepID.ToString());

                processStep.StepName = stepName;
                processStep.ProcessDefID = processDefID;
                processStep.CurrentProcessID = currentProcessID;
                processStep.NextProcessID = nextProcessID;
                processStep = processStep.PostModify();
                return processStep;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        public static ProcessStep DeleteProcessStep(int processStepID)
        {
            try
            {
                var process = new ProcessStep().Find(processStepID.ToString());
                process.Status = ItemStatus.Delete;
                process = process.PostModify();
                return process;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        public static ProcessStep ChangeStatus(int processStepID, int status)
        {
            try
            {
                var process = new ProcessStep().Find(processStepID.ToString());
                process.Status = (ItemStatus)status;
                process = process.PostModify();
                return process;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

       
    }
}
